import os
from model_training import train_decision_tree
from combine_models import combine_risk
from interest_rate_optimization import fit_attrition_curve, optimize_rate

if __name__ == '__main__':
    data1 = '../data/附件1：123家有信贷记录企业的相关数据.xlsx'
    data3 = '../data/附件3：银行贷款年利率与客户流失率关系的统计数据.xlsx'
    model_dir = './models'
    model_path = os.path.join(model_dir, 'decision_tree.pkl')
    preprocessor_path = os.path.join(model_dir, 'preprocessor.pkl')
    annual_credit = 1e8

    # 创建模型存储目录
    os.makedirs(model_dir, exist_ok=True)

    # 1. 训练决策树模型并保存预处理器
    train_decision_tree(data1, model_path, preprocessor_path)

    # 2. 融合风险模型
    df_risk = combine_risk(model_path, preprocessor_path, data1, annual_credit)
    print(df_risk.head())

    # 3. 最优利率及对应流失率与收益
    churn_funcs = fit_attrition_curve(data3)
    for rating, func in churn_funcs.items():
        r_opt, rev_opt = optimize_rate(func)
        churn_opt = func(r_opt)
        print(f"评级 {rating} 最优利率: {r_opt:.4f}, 客户流失率: {churn_opt:.4f}, 最大预期收益: {rev_opt:.4f}")